package com.cmwa.solution.business.productManager.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmwa.solution.base.controller.BaseController;
import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.Result;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.business.prdSellChannel.entity.PrdSellChannelVo;
import com.cmwa.solution.business.product.entity.NavBizVo;
import com.cmwa.solution.business.product.entity.PrdAccountVo;
import com.cmwa.solution.business.product.entity.ProductInfoVo;
import com.cmwa.solution.business.product.entity.ProjectInfoVo;
import com.cmwa.solution.business.product.entity.SubProductInfoVo;
import com.cmwa.solution.business.productManager.service.ProductManagerService;
import com.cmwa.solution.utils.SessionUtils;
import com.cmwa.solution.utils.cached.DictionaryCached;

/**
 * 产品运营Controller
 * @author ex-jiangtao
 * @date 2019-01-22 15:33:47
 */
@Controller
@RequestMapping("/business/productManager")
public class ProductManagerController extends BaseController {

	private final static String JSP_PATH = "business/productManager/";
	
	@Resource
	private ProductManagerService productManagerService;
		
	@RequestMapping("/productManagerOverview")
	public String productManagerOverview(ModelMap model) {
		List<?> prdStatusList = DictionaryCached.getDictionaryByType("SER_PRD_PCS_SAT", "SER_PRD_PCS_SAT", "SER_PRD_PCS_SAT");
		List<?> prdTypeList = DictionaryCached.getDictionaryByType("PRD_CATEGORY", "PRD_CATEGORY", "PRD_CATEGORY");
		List<?> prdTermList = DictionaryCached.getDictionaryByType("PRD_TERM", "PRD_TERM", "PRD_TERM");
		List<?> riskLevelList = DictionaryCached.getDictionaryByType("RISK_LEVEL", "RISK_LEVEL", "RISK_LEVEL");

		model.addAttribute("prdStatusList", prdStatusList);
		model.addAttribute("prdTypeList", prdTypeList);
		model.addAttribute("prdTermList", prdTermList);
		model.addAttribute("riskLevelList", riskLevelList);
		return JSP_PATH+"productManagerOverview";
	}
	
	@RequestMapping("/singleProductOverview")
	public String singleOverview(@Param("prjcode")String prjcode,ModelMap model) {
		ProjectInfoVo projectInfoVo =  productManagerService.getPrdInfoByCode(prjcode);//查询产品数据
		model.addAttribute("projectInfoVo",projectInfoVo);
		NavBizVo navBizVo =  productManagerService.findPrjNavBizData(prjcode);//查询产品净值
		model.addAttribute("navBizVo",navBizVo);
		List<SubProductInfoVo> subInfoList =  productManagerService.findSubProductInfo(prjcode);//查询子产品TA要素
		model.addAttribute("subInfoList",subInfoList);
		List<?> prdStatusList = DictionaryCached.getDictionaryByType("SER_PRD_PCS_SAT", "SER_PRD_PCS_SAT", "SER_PRD_PCS_SAT");
		model.addAttribute("prdStatusList", prdStatusList);
		return JSP_PATH+"singleProductOverview";
	}
	
	@RequestMapping("/disChannelListPage")
	@ResponseBody
	public Page<PrdSellChannelVo> disChannelListPage(SearchParam param) {
		Page<PrdSellChannelVo> list = productManagerService.findPrdDisChannelData(param);
		return list;
	}
	
	@RequestMapping("/accountListPage")
	@ResponseBody
	public Page<PrdAccountVo> accountListPage(SearchParam param) {
		Page<PrdAccountVo> list = productManagerService.findPrdAccountData(param);
		return list;
	}
	
	/*@RequestMapping("/prdFeeListPage")
	@ResponseBody
	public Page<TreFeeDetailVo> prdFeeListPage(SearchParam param) {
		Page<TreFeeDetailVo> list = productManagerService.findPrdFeeData(param);
		return list;
	}*/
	
	@RequestMapping("/productManagerList")
	@ResponseBody
	public Page<ProjectInfoVo> prdInfoListPage(SearchParam param) {
		Page<ProjectInfoVo> list = productManagerService.prdInfoListPage(param);
		return list;
	}
	
	@RequestMapping("/findProductData")
	@ResponseBody
	public List<ProjectInfoVo> findProductData(@Param("prjprcstatStr")String[] prjprcstatStr,HttpServletRequest request) {
		List<ProjectInfoVo> prdInfoList = productManagerService.findProductData(prjprcstatStr);
		return prdInfoList;
	}
	
	/**
	 * 单产品跳转至详情页面
	 * @author ex-jiangtao
	 * @date 2019-01-23 15:33:47
	 * @param model
	 * @return
	 */
	@RequestMapping("/goSingleProductDetail")
	public String goSingleProductDetail(@Param("prdCode")String prdCode,ModelMap model) {
		return JSP_PATH + "singleProductDetail";
	}
	
	
	/**
	 * 系列产品新增页面
	 * @author ex-jiangtao
	 * @date 2019-01-23 15:33:47
	 * @param model
	 * @return
	 */
	@RequestMapping("/projectAdd")
	public String projectAdd(ModelMap model) {
		return JSP_PATH + "projectAdd";
	}
	
	/**
	 * 产品新增页面
	 * @author ex-jiangtao
	 * @date 2019-01-23 15:33:47
	 * @param model
	 * @return
	 */
	@RequestMapping("/productAdd")
	public String productAdd(ModelMap model) {
		return JSP_PATH + "productAdd";
	}
	
	/**
	 * 产品新增页面
	 * @author ex-jiangtao
	 * @date 2019-01-23 15:33:47
	 * @param model
	 * @return
	 */
	@RequestMapping("/subProductAdd")
	public String subProductAdd(ModelMap model) {
		return JSP_PATH + "subProductAdd";
	}
	
	/**
	 * 保存系列产品
	 * @author ex-jiangtao
	 * @date 2019-01-23 15:33:47
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/saveProjectData")
	public String saveProjectData(ProjectInfoVo projectInfoVo,ModelMap model) throws Exception {
		projectInfoVo.setCreateId(SessionUtils.getEmployee().getID());
		// 没有选择经理则默认为当前登录用户
		if (StringUtils.isBlank(projectInfoVo.getManagerId())) {
			projectInfoVo.setManagerId(SessionUtils.getEmployee().getID());
		}
		projectInfoVo.setPrjprcstat("1");//产品状态初始化
		productManagerService.saveProjectData(projectInfoVo);
		
		//添加数据权限
		productManagerService.addAuthority(projectInfoVo);
		return redirectSuccess();
	}
	
	/**
	 * 保存产品
	 * @author ex-jiangtao
	 * @date 2019-01-23 15:33:47
	 * @param model
	 * @return
	 */
	@RequestMapping("/saveProductData")
	public String saveProductData(ProductInfoVo productInfo, ModelMap model) {
		productInfo.setCreateId(SessionUtils.getEmployee().getID());
		productManagerService.saveProductData(productInfo);
		return redirectSuccess();
	}
	
	/**
	 * 保存子产品
	 * @author ex-jiangtao
	 * @date 2019-01-23 15:33:47
	 * @param model
	 * @return
	 */
	@RequestMapping("/saveSubProductData")
	public String saveSubProductData(SubProductInfoVo subProductInfoVo,ModelMap model) {
		subProductInfoVo.setCreateId(SessionUtils.getEmployee().getID());
		subProductInfoVo.setSubPrdPrcStatus("100");
		productManagerService.saveSubProductData(subProductInfoVo);
		return redirectSuccess();
	}
	
	/**
	 * 校验“已有账户”新增账户数据
	 * @param prdAccountVo
	 * @param model
	 * @return
	 */
	@RequestMapping("/checkAddAccountList")
	@ResponseBody
	public Result<T> checkAddAccountList(@RequestParam("accLevel")String accLevel,@RequestParam("accId")String accId,@RequestParam("code")String code,ModelMap model) {
		return productManagerService.checkAddAccountList(accLevel,accId,code);
	}
	
	/**
	 * 增加账户信息
	 * @param prdAccountVo
	 * @param model
	 * @return
	 */
	@RequestMapping("/addPrdAccountList")
	public String addPrdAccountList(PrdAccountVo prdAccountVo,ModelMap model) {
		prdAccountVo.setCreateId(SessionUtils.getEmployee().getID());
		productManagerService.addPrdAccountList(prdAccountVo);
		return redirectSuccess();
	}
	
	/**
	 * 删除账户信息
	 * @param model
	 * @return
	 */
	@RequestMapping("/prdAccountListDel")
	@ResponseBody
	public Result<T> prdAccountListDel(@RequestParam("accIds")String accIds,ModelMap model) {
		Result<T> result = new Result<T>();
		try {
			productManagerService.delPrdAccountList(accIds);
			result.setSuccess(true);
			result.setMessage("账户信息删除成功！");
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMessage("账户信息删除失败！");
		}	
		return result;
	}
	
	/**
	 * 产品管理删除操作页面
	 * @author ex-jiangtao
	 * @date 2019-01-23 15:33:47
	 * @param model
	 * @return
	 */
	@RequestMapping("/productManagerDel")
	public String productManagerDel(ModelMap model) {
		return JSP_PATH + "productManagerDel";
	}
	
	/**
	 * 产品管理修改操作页面
	 * @author ex-jiangtao
	 * @date 2019-01-23 15:33:47
	 * @param model
	 * @return
	 */
	@RequestMapping("/productManagerUpd")
	public String productManagerUpd(ModelMap model) {
		return JSP_PATH + "productManagerUpd";
	}
	
	/**
	 * 银行账户新增操作页面
	 * @author ex-jiangtao
	 * @date 2019-01-23 15:33:47
	 * @param model
	 * @return
	 */
	@RequestMapping("/prdAccountListAdd")
	public String prdAccountListAdd(@Param("prjcode")String prjcode,ModelMap model) {
		model.addAttribute("prjcode", prjcode);
		return JSP_PATH + "prdAccountListAdd";
	}
	
	/**
	 * 产品管理删除操作
	 * @author ex-jiangtao
	 * @date 2019-01-23 15:33:47
	 * @param model
	 * @return
	 */
	@RequestMapping("/delProductManagerData")
	public String productManagerDel(@Param("prjcode")String prjcode,@Param("prdcode")String prdcode,@Param("subPrdcode")String subPrdcode,ModelMap model) {
		productManagerService.productManagerDel(prjcode, prdcode, subPrdcode);
		return redirectSuccess();
	}
	
	/**
	 * 产品管理修改操作
	 * @author ex-jiangtao
	 * @date 2019-01-23 15:33:47
	 * @param model
	 * @return
	 */
	@RequestMapping("/updProductManagerData")
	public String productManagerUpd(@Param("prjcode")String prjcode,@Param("prdcode")String prdcode,@Param("subPrdcode")String subPrdcode,@Param("prjprcstat")String prjprcstat, @Param("subPrdPrcStatus")String subPrdPrcStatus,ModelMap model) {
		SearchParam sp = new SearchParam();
		String modifyId = SessionUtils.getEmployee().getID();//获取当前登录人
		sp.getSp().put("prjcode", prjcode);
		sp.getSp().put("prdcode", prdcode);
		sp.getSp().put("subPrdcode", subPrdcode);
		sp.getSp().put("prjprcstat", prjprcstat);
		sp.getSp().put("subPrdPrcStatus", subPrdPrcStatus);
		sp.getSp().put("modifyId", modifyId);
		productManagerService.productManagerUpd(sp);
		return redirectSuccess();
	}
	
	/**
	 * 产品列表导出
	 * @param sp
	 * @return
	 */
	@RequestMapping("/productDataExport")
	public void prdInfoExportTo(SearchParam sp, HttpServletResponse response) throws IOException{
		//获取当前操作人
		productManagerService.export(response, sp);
	}
	
	@RequestMapping("/querySubPrdInfoBySubPrdCode")
	@ResponseBody
	public Result<SubProductInfoVo> querySubPrdInfoBySubPrdCode(@RequestParam("subPrdCode") String subPrdCode) {
		Result<SubProductInfoVo> result = new Result<>();
		result.setSuccess(true);
		SubProductInfoVo subPrdInfo = productManagerService.querySubPrdInfoBySubPrdCode(subPrdCode);
		if (subPrdInfo == null) {
			result.setSuccess(false);
		} else {
			result.setObject(subPrdInfo);
		}
		return result;
	}
}
